package com.atserver.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.*;
import java.net.Socket;

/**
 * @program: atserver
 * @description: Server Socket for at client
 * @author: Mr.Chen
 * @create: 2022-04-01 02:21
 **/
@Slf4j
public class AtServerSocketThread implements Runnable{

    private Socket socket;

    public AtServerSocketThread(Socket socket) {
        this.socket = socket;
    }

    @Override
    public void run() {
        try(InputStream receive = socket.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(receive));
            PrintWriter sender = new PrintWriter(socket.getOutputStream())
        ){
            String info = "no data";
            while ((info = reader.readLine()) != null){
                System.out.println("client:" + info);
            }
            sender.write("this is at server");
            sender.flush();

        }catch (IOException e){

        }finally {
            try {
                socket.close();
            } catch (IOException e) {

            }
        }
    }
}
